package com.thd.springboottest.i18n.configuration;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        // 这里是从url中获取,也可以从header中获取
//        String l = httpServletRequest.getParameter("i18n");

        // 这里是从header中获取 - 可用于前后端分离无状态访问 带着header中
         String l = httpServletRequest.getHeader("i18n");

        if (StringUtils.isEmpty(l)){ // 如果没有获取到语言设置则使用默认的语言
            Locale locale = Locale.getDefault();
            return locale;
        }else {
            String[] split = l.split("_");
            return new Locale(split[0],split[1]);
        }
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
        System.out.println("|||||||||||||||||||||||||||||||||||||");

    }
}
